package com.tp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;

import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Hotel extends CustomWindow implements OnClickListener, TextWatcher {
	// EditText hotelName, hotelDes;
	AutoCompleteTextView hotelName;
	TextView hotel_selected;
	Button chooseHotel;
	Button submit;
	static int flag; // flag =1 goi tu Create, 2 goi tu EditTrip
	static JSONArray jArr;
	ArrayList<String> arr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hotel);
		this.icon.setImageResource(R.drawable.hotel);
		this.title.setText("Hotel");
		submit = (Button) findViewById(R.id.submit_hotel);
		submit.setOnClickListener(this);
		hotelName = (AutoCompleteTextView) findViewById(R.id.hotel_name);
		hotel_selected = (TextView) findViewById(R.id.hotel_selected);
		arr = getHotel();
		hotelName.addTextChangedListener(this);
		hotelName.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, arr));

	}

	public ArrayList<String> getHotel() {
		// END Convert response to string
		JSONArray jArray;
		JSONObject json_data = null;
		int i;
		ArrayList<String> r = new ArrayList<String>();
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters
				.add(new BasicNameValuePair("id_Type", String.valueOf(1)));
		String result = null;
		try {
			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/ObjectController/getAllObjectByType", postParameters);

			jArray = new JSONArray(result);
			jArr = jArray;
			for (i = 0; i < jArray.length(); i++) {
				json_data = jArray.getJSONObject(i);
				// Log.d("A", json_data.getString("name"));
				r.add(json_data.getString("name"));
			}
			// return r;
		} catch (JSONException e1) {
			Toast.makeText(this, "getHotel" + e1.toString(), Toast.LENGTH_SHORT)
					.show();
		} catch (ParseException e1) {
			Toast.makeText(this, "getHotel" + e1.toString(), Toast.LENGTH_SHORT)
					.show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return r;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.text.TextWatcher#afterTextChanged(android.text.Editable)
	 */
	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.text.TextWatcher#beforeTextChanged(java.lang.CharSequence,
	 * int, int, int)
	 */
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.text.TextWatcher#onTextChanged(java.lang.CharSequence, int,
	 * int, int)
	 */
	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		hotel_selected.setText(hotelName.getText());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_hotel_, menu);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case R.id.submit_hotel:
			int i;

			for (i = 0; i < arr.size(); i++) {
				if (arr.get(i).equals(hotelName.getText().toString()))
					break;

			}
			if (i == arr.size())
				addNewHotel(hotelName.getText().toString());
			if (flag == 1) {
				Create.items[0] += "\n" + hotelName.getText().toString();
				Create.tripName.setText(Create.name);
				Create.startDate.setText(Create.date);
				startActivity(new Intent(this, Create.class));
			} else {
				EditTrip.ini = 0;
				EditTrip.items[0] += "\n" + hotelName.getText().toString();
				Log.v("ItemO", hotelName.getText().toString());
				EditTrip.tripName.setText(EditTrip.name);
				EditTrip.startDate.setText(EditTrip.date);
				startActivity(new Intent(this, EditTrip.class));
			}
			break;

		}
	}

	private void addNewHotel(String string) {
		// TODO Auto-generated method stub

		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("name", string));
		postParameters.add(new BasicNameValuePair("id_User", Integer
				.toString(Constant.user)));
		postParameters
				.add(new BasicNameValuePair("id_Type", String.valueOf(1)));
		String result = null;
		try {

			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/ObjectController/addBasicObject", postParameters);
			String res = result.toString();

			// res = res.trim();
			res = res.replaceAll("\\s+", "");
			if (Integer.parseInt(res) > 0) {
				Toast.makeText(this, "Add object successful",
						Toast.LENGTH_SHORT).show();
				addHotelToArray(Integer.parseInt(res));

			} else {
				Toast.makeText(this, "Add object fail", Toast.LENGTH_SHORT)
						.show();
			}

		} catch (Exception e) {

		}

	}

	private void addHotelToArray(int id_Hotel) {
		// TODO Auto-generated method stub

		JSONArray jArray;
		JSONObject json_data = null;
		int i;

		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("id_Obj", String
				.valueOf(id_Hotel)));
		String result = null;
		try {
			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/ObjectController/getObjectById", postParameters);

			jArray = new JSONArray(result);
			for (i = 0; i < jArray.length(); i++) {
				json_data = jArray.getJSONObject(i);
				// Log.d("A", json_data.getString("name"));
				jArr.put(json_data);
			}
		} catch (JSONException e1) {
			Toast.makeText(this, "addHotelToArray" + e1.toString(),
					Toast.LENGTH_SHORT).show();
		} catch (ParseException e1) {
			Toast.makeText(this, "addHotelToArray" + e1.toString(),
					Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
